<template>
  <el-card class="loading-card" :class="{ 'is-loading': loading }">
    <template #header v-if="title">
      <div class="card-header">
        <span>{{ title }}</span>
        <el-button 
          v-if="showRefresh" 
          :icon="Refresh" 
          :loading="loading" 
          @click="handleRefresh"
          size="small"
          text
        />
      </div>
    </template>

    <div class="card-content">
      <div v-if="loading" class="loading-overlay">
        <el-icon class="loading-icon" :size="32">
          <Loading />
        </el-icon>
        <p class="loading-text">{{ loadingText }}</p>
      </div>
      
      <div v-else class="content-wrapper">
        <slot />
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { Refresh, Loading } from '@element-plus/icons-vue'

interface Props {
  title?: string
  loading?: boolean
  loadingText?: string
  showRefresh?: boolean
}

interface Emits {
  refresh: []
}

const props = withDefaults(defineProps<Props>(), {
  title: '',
  loading: false,
  loadingText: '加载中...',
  showRefresh: false
})

const emit = defineEmits<Emits>()

const handleRefresh = () => {
  emit('refresh')
}
</script>

<style scoped>
.loading-card {
  position: relative;
  transition: all 0.3s ease;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}

.card-content {
  position: relative;
  min-height: 100px;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);
  z-index: 10;
}

.loading-icon {
  color: #409eff;
  animation: rotate 2s linear infinite;
}

.loading-text {
  margin-top: 12px;
  color: #606266;
  font-size: 14px;
}

.content-wrapper {
  opacity: 1;
  transition: opacity 0.3s ease;
}

.is-loading .content-wrapper {
  opacity: 0.3;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
